package com.xw.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * request对象
 * 		作用：封存了当前前请求的所有请求信息
 * 		使用：
 * 			获取请求头数据
 * 			获取请求行数据
 * 			获取请求请求体数据（用户数据）
 * 		注意：request对象由Tomcat服务创建，并作为实参传递给servlet的service方法
 * @author Administrator
 *
 */
public class RequestServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//获取请求头数据
		String m=req.getMethod();
		System.out.println("method:"+m);
		StringBuffer url=req.getRequestURL();
		System.out.println("url:"+url);
		String h=req.getScheme();
		System.out.println("协议："+h);
		
		//获取请求行数据
		Enumeration e=req.getHeaderNames();
		while (e.hasMoreElements()) {
			String name = (String) e.nextElement();
			String value=req.getHeader(name);
			System.out.println("header name:"+name+" value:"+value);
		}
		
		//获取请求体数据
		String name=req.getParameter("uname");
		System.out.println(name);
		String pwd=req.getParameter("pass");
		System.out.println(pwd);
		String[] fav=req.getParameterValues("fav");
		for (String string : fav) {
			System.out.println(string);
		}
	}
}
